package com.fyh.controller;

import com.fyh.entity.chat.ChatPageProp;
import com.fyh.entity.chat.ChatRecord;
import com.fyh.entity.chat.InformRecord;
import com.fyh.entity.chat.SimpleContact;
import com.fyh.entity.common.Result;
import com.fyh.entity.job.DeliveryRecord;
import com.fyh.service.ChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chat")
@Slf4j
public class ChatController {

    @Autowired
    ChatService chatService;

    /**
     * 获取联系人列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/getContactList")
    public Result<List<SimpleContact>> getSimpleContactList(@RequestHeader("userId") Long userId) {
        log.info("获取联系人列表");
        List<SimpleContact> getSimpleContactList = chatService.getSimpleContactList(userId);
        return Result.success(getSimpleContactList);
    }

    /**
     * 用户点击联系人打开聊天窗口后，更新聊天记录状态为已读
     * @param receiverId
     */
    @PutMapping("/updateChatRecordStatus")
    public Result updateChatRecordStatus(@RequestParam("sendId") Long sendId, @RequestHeader("userId") Long receiverId) {
        log.info("更新{}发送给{}的聊天记录状态为已读", sendId, receiverId);
        chatService.updateChatRecordStatus(sendId, receiverId);
        return Result.success();
    }

    /**
     * 分页查询聊天记录
     * @param chatPageProp
     * @return
     */
    @PostMapping("/getChatRecordList")
    public Result<List<ChatRecord>> getChatRecordList(@RequestBody ChatPageProp chatPageProp, @RequestHeader("userId") Long userId) {
        log.info("分页查询聊天记录: {}", chatPageProp);
        chatPageProp.setUserId(userId);
        List<ChatRecord> chatRecordList = chatService.getChatRecordList(chatPageProp);
        return Result.success(chatRecordList);
    }

    /**
     * 添加联系人
     * @return
     */
    @PutMapping("/addContact")
    public Result addChatContact(@RequestParam("contactId") Long contactId, @RequestHeader("userId") Long userId){
        log.info("招聘者：{}添加应聘者: {}", userId, contactId);
        chatService.addChatContact(userId, contactId);
        return Result.success();
    }

    /**
     * 查询是否有未读消息
     */
    @GetMapping("/getUnreadMessage")
    public Result<Boolean> getUnreadMessage(@RequestHeader("userId") Long userId){
        log.info("查询是否有未读消息");
        return Result.success(chatService.getUnreadMessage(userId));
    }

    /**
     * 修改联系人备注
     */
    @PostMapping("/updateContactName")
    public Result updateContactName(@RequestBody SimpleContact simpleContact, @RequestHeader("userId") Long userId){
        log.info("修改联系人备注: {}", simpleContact);
        chatService.updateContactName(simpleContact, userId);
        return Result.success();
    }

    /**
     * 发送职位投递消息
     *
     * @param positionId
     * @param employerId
     * @return
     */
    @PutMapping("/sendDeliveryMessage")
    public Result sendDeliveryMessage(@RequestParam("positionId") Long positionId, @RequestParam("employerId") Long employerId){
        log.info("发送职位投递消息: {}, {}", positionId, employerId);
        chatService.sendDeliveryMessage(positionId, employerId);
        return Result.success();
    }

    /**
     * 获取未读通知数量
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUnreadInform")
    public Result<Integer> getUnreadInform(@RequestHeader("userId") Long userId){
        log.info("获取是否存在未读通知");
        Integer unreadInform = chatService.getUnreadInform(userId);
        return Result.success(unreadInform);
    }

    /**
     * 获取用户通知记录
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserInformRecord")
    public Result<List<InformRecord>> getUserInformRecord(@RequestParam("offset") Integer offset, @RequestHeader("userId") Long userId){
        log.info("获取用户通知记录");
        List<InformRecord> informRecordList = chatService.getUserInformRecord(offset, userId);
        return Result.success(informRecordList);
    }

    /**
     * 发送投递状态变更通知给投递者
     *
     * @param deliveryRecord
     */
    @PostMapping("/sendDeliveryStatusInform")
    public Result sendDeliveryStatusInform(@RequestBody DeliveryRecord deliveryRecord){
        log.info("发送投递状态变更通知给投递者: {}", deliveryRecord);
        chatService.sendDeliveryStatusInform(deliveryRecord);
        return Result.success();
    }

    /**
     * 更新用户通知状态为已读
     *
     * @param userId
     */
    @PutMapping("/updateInformStatus")
    public Result updateInformStatus(@RequestHeader("userId") Long userId){
        log.info("更新用户通知状态为已读");
        chatService.updateInformStatus(userId);
        return Result.success();
    }
}
